/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package slisfx.xml;

import java.io.*;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
import slisfx.Globals;
import slisfx.user.Userbase;
import slisfx.user.Users;


/**
 * The SAXReader class reads an xml file to store data back into the system.
 * @author Rebecca
 */

public class SAXReader implements Globals {

	/**
	 * saxReader reads the file database.xml.
	 * @throws SAXException the sAX exception
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	public void saxReader(){
		try{
			XMLReader reader = XMLReaderFactory.createXMLReader();
			reader.setContentHandler(new MyContentHandler());
                        //InputStream configStream = getClass().getResourceAsStream("database.xml");
                        //reader.parse(new InputSource(configStream));  
                        reader.parse("database.xml");
		}catch(FileNotFoundException e){
			System.out.println("The file was not found.");
		}catch(SAXException e){
			System.out.println("Error with SAX Handler");
		}catch (IOException e){
			System.out.println("Error with IO");
		}
	}
	
	/**
	 * The MyContentHandler class overrides method in the DefaultHandler class. 
	 * Instantiates new tables with fields that are stored in the database. 
	 */
	private class MyContentHandler extends DefaultHandler {

		private boolean userbase = false;
                private boolean user = false;
                private boolean username = false;
		private boolean password = false;
		private boolean permission = false;
                private Users savedUser; 
                private String uName;
                private String pWord;
                private String permis;
		
		/* (non-Javadoc)
		 * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
		 */
		@Override
		public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
				if(qName.equalsIgnoreCase(sUserbase)){
					userbase = true;
				}
                                else if(qName.equalsIgnoreCase(sUser)){
					user = true;
				}
                                else if(qName.equalsIgnoreCase(sUsername)){
                                        username = true;
                                        uName = new String();
                                }
				else if(qName.equalsIgnoreCase(sPassword)){
					password = true;
                                        pWord = new String();
				}
				else if(qName.equalsIgnoreCase(sPermission)){
					permission = true;
				}
			}
				
		/* (non-Javadoc)
		 * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
		 */
		@Override
		public void endElement(String uri, String localName, String qName)throws SAXException {
			if(permission){
                            permission = false;
			}	
			else if(password){
                            password = false;
			}
                        else if(username){
                            username = false;
                        }
                        else if (user){
                            user = false;
                            savedUser = new Users(uName, pWord, permis);
                            Userbase.getUserbase().add_user(savedUser);
                        }
			else if(userbase) {
				userbase = false;
			}
		}
	

		/* (non-Javadoc)
		 * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
		 */
		@Override
		public void characters(char[] ch, int start, int length)throws SAXException {
			String data = new String(ch, start, length); 
			if(userbase) {	
				if (user){	
                                    if(username){
                                        uName = data;
                                    }
                                    else if(password){
                                        pWord = data;
                                    }
                                    else if(permission){
                                        permis = data; 
                                    }	
				}
			}
		}	
	}
}
